package com.apes.autoparts.parts.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SubGroup.CONST_TABLE_NAME)
public class SubGroup extends Model {
    public static final String CONST_TABLE_NAME = "auto_sub_group";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "子组")
    @Comment("唯一码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "org_selector_id", referencedColumnName = "id")
    @Comment("车型")
    private AutoModel orgSelectorId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "main_group_id", referencedColumnName = "id")
    @Comment("主组")
    private MainGroup mainGroupId;

    @Column(name = "sub_group_id", length = 32)
    @Comment("子组编码")
    private String subGroupId;

    @Column(name = "code", length = 32)
    @Comment("子组code")
    private String code;

    @Column(name = "name")
    @Comment("子组名称")
    private String name;

    @Column(name = "description", length = 4000)
    @Comment("描述")
    private String description;

    @Column(name = "remark", length = 4000)
    @Comment("备注")
    private String remark;

    @Column(name = "org_image_url", length = 4000)
    @Comment("原始子组图片")
    private String orgImageUrl;

    @Column(name = "image_url", length = 4000)
    @Comment("服务器子组图片地址")
    private String imageUrl;

    @Column(name = "image_sn")
    @Comment("图号")
    private String imageSN;

    @Column(name = "applicable_model",length = 4000)
    @Comment("型号")
    private String applicableModel;

    @Column(name = "has_next", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("下级标记")
    private boolean hasNext = true;

    @Column(name = "belong_to", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("属于此vin")
    private boolean belongTo = true;

    @Column(name = "selector_Id", length = 32)
    @Comment("车型")
    private String selectorId;

    @Column(name = "timestamp")
    @Comment("时间戳")
    private Date timestamp;

    @Column(name = "main_group_code")
    @Comment("主组编号")
    private String mainGroupCode;

    @Transient
    private String realSelectorId;

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid = true;

    @Column(name = "note", length = 1000)
    @Comment("备注")
    private String note; //备注

    public String getRealSelectorId() {
        return this.selectorId != null ? this.selectorId : this.orgSelectorId.getSelectorId();
    }

    /***
     * 图片
     */
    @OneToMany(mappedBy = "subGroupId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SubGroupImg> subGroupImgs = new ArrayList<>();

    public List<SubGroupImg> getSubGroupImgs() {
        for (SubGroupImg item : subGroupImgs) {
            item.setSubGroupId(this);
        }
        return subGroupImgs;
    }


}
